import 'dart:math';

import 'package:detect/entity/entity.dart';
import 'package:detect/notifier/fpga_time.dart';
import 'package:detect/notifier/state.dart';
import 'package:detect/utils/db_util.dart';
import 'package:detect/utils/detect_util.dart';
import 'package:detect/utils/global.dart';
import 'package:detect/utils/image_util.dart';
import 'package:detect/utils/loading_util.dart';
import 'package:detect/utils/modal_util.dart';
import 'package:detect/utils/option_util.dart';
import 'package:detect/utils/toast_util.dart';
import 'package:detect/widgets/custom_table.dart';
import 'package:detect/widgets/date_picker.dart';
import 'package:detect/widgets/dropdown.dart';
import 'package:detect/widgets/future_widget.dart';
import 'package:detect/widgets/input_field.dart';
import 'package:detect/widgets/radio_tile.dart';
import 'package:detect/widgets/scroll_listener.dart';
import 'package:detect/widgets/triangle.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:provider/provider.dart';

class DetectGeneral extends StatefulWidget {
  const DetectGeneral({super.key});

  @override
  State<DetectGeneral> createState() => _DetectGeneralState();
}

class _DetectGeneralState extends State<DetectGeneral> {
  @override
  Widget build(BuildContext context) => _DetectWrap(
        navList: const ["连续探测任务\n方案设置", "连续探测结果\n查询生成", "优选结果生成\n列表查询"],
        builder: (context, showPopover, index) => [
          _DetectPanel1(
            onAppend: (stateSetter) {
              String? name, role;
              int? power, radioId, disabledMHzId;
              double? startMHz, endMHz;
              ModalUtil.drawer(
                context: context,
                title: "添加连续探测方案",
                width: Global.ds(620),
                radius: Global.ds(16),
                barrierDismissible: false,
                child: Padding(
                  padding: EdgeInsets.all(Global.ds(20)),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("方案角色：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: StatefulBuilder(
                              builder: (context, stateSetter) => Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: DetectRole.values
                                    .map<RadioTile<String>>((e) => RadioTile<String>(
                                          value: e.name,
                                          groupValue: role,
                                          label: Text(e.label, style: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#FAFAFA"))),
                                          size: Global.ds(20),
                                          onCheck: (value) {
                                            stateSetter(() {
                                              role = value;
                                            });
                                          },
                                        ))
                                    .toList(),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("方案名称：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: InputField(
                                key: UniqueKey(),
                                hintText: "请输入方案名称",
                                textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                                hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                                onChanged: (controller, focusNode, value) => name = value.trim(),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("探测台站：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: FutureWidget(
                                future: DBApi.radio.list("id != ?", [context.read<StateNotifier>().localId]),
                                doneBuilder: (context, snapshot) => Dropdown<int>(
                                  key: UniqueKey(),
                                  options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text(e.name))).toList(),
                                  hintText: "请选择站台",
                                  fontSize: Global.ds(18),
                                  fontColor: ColorUtil.color("#ABDBF3"),
                                  dropdownColor: ColorUtil.color("#1A263D"),
                                  onChanged: (value) => radioId = value,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("禁用频率：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: FutureWidget(
                                future: DBApi.disabledMHz.list(),
                                doneBuilder: (context, snapshot) => Dropdown<int>(
                                  key: UniqueKey(),
                                  options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text("禁用频率表V${e.id}"))).toList(),
                                  hintText: "请选择版本",
                                  fontSize: Global.ds(18),
                                  fontColor: ColorUtil.color("#ABDBF3"),
                                  dropdownColor: ColorUtil.color("#1A263D"),
                                  onChanged: (value) => disabledMHzId = value,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("探测功率：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: Dropdown<int>(
                                key: UniqueKey(),
                                options: PowerType.values.map((e) => DropdownMenuItem<int>(value: e.index, child: Text(e.name))).toList(),
                                hintText: "请选择功率",
                                fontSize: Global.ds(18),
                                fontColor: ColorUtil.color("#ABDBF3"),
                                dropdownColor: ColorUtil.color("#1A263D"),
                                onChanged: (value) => power = value,
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("频率范围：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: InputField(
                                key: UniqueKey(),
                                hintText: "请输入起始频率范围",
                                keyboardType: const TextInputType.numberWithOptions(decimal: true),
                                textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                                hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                                suffix: const Text("MHz"),
                                onChanged: (controller, focusNode, value) => startMHz = double.tryParse(value),
                              ),
                            ),
                          ),
                          VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                          ImageUtil.asset("static/images/icon/10.png", width: Global.ds(28), height: Global.ds(9)),
                          VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: InputField(
                                key: UniqueKey(),
                                hintText: "请输入结束频率范围",
                                keyboardType: const TextInputType.numberWithOptions(decimal: true),
                                textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                                hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                                suffix: const Text("MHz"),
                                onChanged: (controller, focusNode, value) => endMHz = double.tryParse(value),
                              ),
                            ),
                          ),
                        ],
                      ),
                      const Spacer(),
                      Align(
                        alignment: Alignment.centerRight,
                        child: TextButton(
                          onPressed: () {
                            if (name?.isEmpty ?? true) {
                              ToastUtil.error("请输入方案名称");
                            } else if (role == null) {
                              ToastUtil.error("请选择方案角色");
                            } else if (radioId == null) {
                              ToastUtil.error("请选择探测站台");
                            } else if (disabledMHzId == null) {
                              ToastUtil.error("请选择禁用频率");
                            } else if (power == null) {
                              ToastUtil.error("请选择探测功率");
                            } else if (startMHz == null) {
                              ToastUtil.error("请选择起始频率范围");
                            } else if ((startMHz ?? 0) <= 0) {
                              ToastUtil.error("起始频率范围必须大于0");
                            } else if (endMHz == null) {
                              ToastUtil.error("请选择结束频率范围");
                            } else if ((endMHz ?? 0) <= 0) {
                              ToastUtil.error("结束频率范围必须大于0");
                            } else {
                              String time = context.read<FpgaTimeNotifier>().format("yyyy-MM-dd HH:mm:ss");
                              LoadingUtil.show(
                                desc: "正在创建连续探测任务",
                                onLoading: (loading) {
                                  DBApi.detectTask.insert(DetectTaskEntity(radioId!, TaskType.general.name, role!, name!, time, disabledMHzId!, 0)).then((value) {
                                    if (value != null) {
                                      int startHz = (startMHz! * 1000000).round(), endHz = (endMHz! * 1000000).round();
                                      DBApi.generalTaskHours.insert(List.generate(24, (hour) => GeneralTaskHoursEntity(value.id!, hour, startHz, endHz, power!))).then((value) {
                                        stateSetter(() {});
                                        Navigator.pop(context);
                                        ToastUtil.success("添加成功");
                                        loading.close();
                                      });
                                    }
                                  });
                                },
                              );
                            }
                          },
                          style: TextButton.styleFrom(
                            fixedSize: Size(Global.ds(100), Global.ds(40)),
                            textStyle: TextStyle(fontSize: Global.ds(18)),
                            foregroundColor: Colors.white,
                            backgroundColor: ColorUtil.color("#2CA6E0"),
                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                          ),
                          child: const Text("确定"),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
            builder: (name, startTime, endTime, stateSetter) => FutureWidget<List<DetectTaskEntity>>(
              future: DBApi.detectTask.list(TaskType.general, name, startTime, endTime),
              doneBuilder: (context, snapshot) => ListView.separated(
                padding: EdgeInsets.zero,
                itemCount: snapshot.requireData.length,
                itemBuilder: (BuildContext context, int index) => _DetectTaskItem(
                  entity: snapshot.requireData[index],
                  onTapDisabledMHz: (offset, taskId, values) {
                    showPopover(taskId, offset, Text(values.replaceAll(",", "、"), style: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#0F3A4E"))));
                  },
                  onEdit: (entity) => ModalUtil.drawer(
                    context: context,
                    title: "编辑连续探测方案",
                    width: Global.ds(620),
                    radius: Global.ds(16),
                    barrierDismissible: false,
                    child: Padding(
                      padding: EdgeInsets.all(Global.ds(20)),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          Row(
                            children: [
                              Container(
                                width: Global.ds(86),
                                height: Global.ds(40),
                                alignment: Alignment.centerRight,
                                child: Text("方案角色：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                              ),
                              Expanded(
                                child: StatefulBuilder(
                                  builder: (context, stateSetter) => Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: DetectRole.values
                                        .map<RadioTile<String>>((e) => RadioTile<String>(
                                              value: e.name,
                                              groupValue: entity.role,
                                              label: Text(e.label, style: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#FAFAFA"))),
                                              size: Global.ds(20),
                                              onCheck: (value) {
                                                stateSetter(() {
                                                  entity.role = value;
                                                });
                                              },
                                            ))
                                        .toList(),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          Divider(height: Global.ds(20), color: Colors.transparent),
                          Row(
                            children: [
                              Container(
                                width: Global.ds(86),
                                height: Global.ds(40),
                                alignment: Alignment.centerRight,
                                child: Text("方案名称：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                              ),
                              Expanded(
                                child: Container(
                                  height: Global.ds(40),
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                  decoration: BoxDecoration(
                                    color: ColorUtil.color("#366485"),
                                    border: Border.all(color: ColorUtil.color("#3B99CC")),
                                    borderRadius: BorderRadius.circular(Global.ds(20)),
                                  ),
                                  child: InputField(
                                    key: UniqueKey(),
                                    hintText: "请输入方案名称",
                                    initialValue: entity.name,
                                    textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                                    hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                                    onChanged: (controller, focusNode, value) => entity.name = value.trim(),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          Divider(height: Global.ds(20), color: Colors.transparent),
                          Row(
                            children: [
                              Container(
                                width: Global.ds(86),
                                height: Global.ds(40),
                                alignment: Alignment.centerRight,
                                child: Text("探测台站：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                              ),
                              Expanded(
                                child: Container(
                                  height: Global.ds(40),
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                  decoration: BoxDecoration(
                                    color: ColorUtil.color("#366485"),
                                    border: Border.all(color: ColorUtil.color("#3B99CC")),
                                    borderRadius: BorderRadius.circular(Global.ds(20)),
                                  ),
                                  child: FutureWidget(
                                    future: DBApi.radio.list("id != ?", [context.read<StateNotifier>().localId]),
                                    doneBuilder: (context, snapshot) => Dropdown<int>(
                                      key: UniqueKey(),
                                      options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text(e.name))).toList(),
                                      hintText: "请选择站台",
                                      initialValue: entity.radioId,
                                      fontSize: Global.ds(18),
                                      fontColor: ColorUtil.color("#ABDBF3"),
                                      dropdownColor: ColorUtil.color("#1A263D"),
                                      onChanged: (value) => entity.radioId = value!,
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          Divider(height: Global.ds(20), color: Colors.transparent),
                          Row(
                            children: [
                              Container(
                                width: Global.ds(86),
                                height: Global.ds(40),
                                alignment: Alignment.centerRight,
                                child: Text("禁用频率：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                              ),
                              Expanded(
                                child: Container(
                                  height: Global.ds(40),
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                  decoration: BoxDecoration(
                                    color: ColorUtil.color("#366485"),
                                    border: Border.all(color: ColorUtil.color("#3B99CC")),
                                    borderRadius: BorderRadius.circular(Global.ds(20)),
                                  ),
                                  child: FutureWidget(
                                    future: DBApi.disabledMHz.list(),
                                    doneBuilder: (context, snapshot) => Dropdown<int>(
                                      key: UniqueKey(),
                                      options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text("禁用频率表V${e.id}"))).toList(),
                                      hintText: "请选择版本",
                                      initialValue: entity.disabledMHzId,
                                      fontSize: Global.ds(18),
                                      fontColor: ColorUtil.color("#ABDBF3"),
                                      dropdownColor: ColorUtil.color("#1A263D"),
                                      onChanged: (value) => entity.disabledMHzId = value!,
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          const Spacer(),
                          Align(
                            alignment: Alignment.centerRight,
                            child: TextButton(
                              onPressed: () {
                                if (entity.name.isEmpty) {
                                  ToastUtil.error("请输入方案名称");
                                } else {
                                  DBApi.detectTask.update(entity).then((value) {
                                    setState(() {});
                                    Navigator.pop(context);
                                    ToastUtil.success("修改成功");
                                  });
                                }
                              },
                              style: TextButton.styleFrom(
                                fixedSize: Size(Global.ds(100), Global.ds(40)),
                                textStyle: TextStyle(fontSize: Global.ds(18)),
                                foregroundColor: Colors.white,
                                backgroundColor: ColorUtil.color("#2CA6E0"),
                                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                              ),
                              child: const Text("确定"),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  onDel: (detectTaskId) => ModalUtil.confirm(content: "方案删除后不可恢复，确定要删除吗？").then((value) {
                    if (value == true) {
                      DBApi.detectTask.delete(detectTaskId).then((value) {
                        stateSetter(() {});
                        ToastUtil.success("删除成功");
                      });
                    }
                  }),
                  onHoursDetail: (entity) => ModalUtil.drawer(
                    context: context,
                    title: "24小时详情表-${entity.name}",
                    alignment: DrawerAlignment.start,
                    radius: Global.ds(16),
                    resizeToAvoidBottomInset: false,
                    child: StatefulBuilder(
                      builder: (context, stateSetter) => FutureWidget<List<GeneralTaskHoursEntity>>(
                        future: DBApi.generalTaskHours.list("detect_task_id = ?", [entity.id]),
                        doneBuilder: (context, snapshot) => CustomTable<GeneralTaskHoursEntity>(
                          data: snapshot.requireData,
                          fields: const [Text("时段"), Text("频段(MHz)"), Text("功率"), Text("操作")],
                          headSize: Global.ds(40),
                          itemSize: Global.ds(60),
                          hoverColor: ColorUtil.color("#46E5CB").withOpacity(.5),
                          headColor: ColorUtil.color("#1A263D"),
                          itemBorder: Border(bottom: BorderSide(color: ColorUtil.color("#1A263D"))),
                          headTextStyle: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#036EB7"), overflow: TextOverflow.ellipsis),
                          itemTextStyle: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#2CA6E0"), overflow: TextOverflow.ellipsis),
                          itemBuilder: (context, index, data) => [
                            Text(data.hours),
                            Text("${Global.toFixed(data.startHz / 1000000, 3)}-${Global.toFixed(data.endHz / 1000000, 3)}"),
                            Text(PowerType.values[data.power].name),
                            IconButton(
                              onPressed: () {
                                double? startMHz = Global.toFixed(data.startHz / 1000000, 3), endMHz = Global.toFixed(data.endHz / 1000000, 3);
                                int? power = data.power;
                                ModalUtil.drawer(
                                  context: context,
                                  title: "编辑时段(${data.hours})",
                                  width: Global.ds(620),
                                  radius: Global.ds(16),
                                  barrierDismissible: false,
                                  child: Padding(
                                    padding: EdgeInsets.all(Global.ds(20)),
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment: CrossAxisAlignment.stretch,
                                      children: [
                                        Row(
                                          children: [
                                            Container(
                                              width: Global.ds(86),
                                              height: Global.ds(40),
                                              alignment: Alignment.centerRight,
                                              child: Text(
                                                "探测功率：",
                                                style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")),
                                                textAlign: TextAlign.right,
                                              ),
                                            ),
                                            Expanded(
                                              child: Container(
                                                height: Global.ds(40),
                                                alignment: Alignment.centerLeft,
                                                padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                                decoration: BoxDecoration(
                                                  color: ColorUtil.color("#366485"),
                                                  border: Border.all(color: ColorUtil.color("#3B99CC")),
                                                  borderRadius: BorderRadius.circular(Global.ds(20)),
                                                ),
                                                child: Dropdown<int>(
                                                  key: UniqueKey(),
                                                  options: PowerType.values.map((e) => DropdownMenuItem<int>(value: e.index, child: Text(e.name))).toList(),
                                                  hintText: "请选择功率",
                                                  initialValue: power,
                                                  fontSize: Global.ds(18),
                                                  fontColor: ColorUtil.color("#ABDBF3"),
                                                  dropdownColor: ColorUtil.color("#1A263D"),
                                                  onChanged: (value) => power = value,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                        Divider(height: Global.ds(20), color: Colors.transparent),
                                        Row(
                                          children: [
                                            Container(
                                              width: Global.ds(86),
                                              height: Global.ds(40),
                                              alignment: Alignment.centerRight,
                                              child: Text("频率范围：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                                            ),
                                            Expanded(
                                              child: Container(
                                                height: Global.ds(40),
                                                alignment: Alignment.centerLeft,
                                                padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                                decoration: BoxDecoration(
                                                  color: ColorUtil.color("#366485"),
                                                  border: Border.all(color: ColorUtil.color("#3B99CC")),
                                                  borderRadius: BorderRadius.circular(Global.ds(20)),
                                                ),
                                                child: InputField(
                                                  key: UniqueKey(),
                                                  hintText: "请输入起始频率范围",
                                                  initialValue: startMHz.toString(),
                                                  keyboardType: const TextInputType.numberWithOptions(decimal: true),
                                                  textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                                                  hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                                                  suffix: const Text("MHz"),
                                                  onChanged: (controller, focusNode, value) => startMHz = double.tryParse(value),
                                                ),
                                              ),
                                            ),
                                            VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                                            ImageUtil.asset("static/images/icon/10.png", width: Global.ds(28), height: Global.ds(9)),
                                            VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                                            Expanded(
                                              child: Container(
                                                height: Global.ds(40),
                                                alignment: Alignment.centerLeft,
                                                padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                                decoration: BoxDecoration(
                                                  color: ColorUtil.color("#366485"),
                                                  border: Border.all(color: ColorUtil.color("#3B99CC")),
                                                  borderRadius: BorderRadius.circular(Global.ds(20)),
                                                ),
                                                child: InputField(
                                                  key: UniqueKey(),
                                                  hintText: "请输入结束频率范围",
                                                  initialValue: endMHz.toString(),
                                                  keyboardType: const TextInputType.numberWithOptions(decimal: true),
                                                  textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                                                  hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                                                  suffix: const Text("MHz"),
                                                  onChanged: (controller, focusNode, value) => endMHz = double.tryParse(value),
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                        const Spacer(),
                                        Align(
                                          alignment: Alignment.centerRight,
                                          child: TextButton(
                                            onPressed: () {
                                              if (power == null) {
                                                ToastUtil.error("请选择探测功率");
                                              } else if (startMHz == null) {
                                                ToastUtil.error("请选择起始频率范围");
                                              } else if ((startMHz ?? 0) <= 0) {
                                                ToastUtil.error("起始频率范围必须大于0");
                                              } else if (endMHz == null) {
                                                ToastUtil.error("请选择结束频率范围");
                                              } else if ((endMHz ?? 0) <= 0) {
                                                ToastUtil.error("结束频率范围必须大于0");
                                              } else {
                                                data.power = power!;
                                                data.startHz = (startMHz! * 1000000).round();
                                                data.endHz = (endMHz! * 1000000).round();
                                                DBApi.generalTaskHours.update(data).then((value) {
                                                  stateSetter(() {});
                                                  Navigator.pop(context);
                                                  ToastUtil.success("修改成功");
                                                });
                                              }
                                            },
                                            style: TextButton.styleFrom(
                                              fixedSize: Size(Global.ds(100), Global.ds(40)),
                                              textStyle: TextStyle(fontSize: Global.ds(18)),
                                              foregroundColor: Colors.white,
                                              backgroundColor: ColorUtil.color("#2CA6E0"),
                                              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                                            ),
                                            child: const Text("确定"),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                              icon: ImageUtil.icon("static/images/icon/5.png"),
                              style: IconButton.styleFrom(iconSize: Global.ds(24), foregroundColor: ColorUtil.color("#66FFFB")),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  onResultDetail: (entity) => ModalUtil.drawer(
                    context: context,
                    title: "探测结果-${entity.name}",
                    alignment: DrawerAlignment.start,
                    radius: Global.ds(16),
                    resizeToAvoidBottomInset: false,
                    child: StatefulBuilder(
                      builder: (context, stateSetter) => FutureWidget<List<GeneralTaskResultEntity>>(
                        future: DBApi.generalTaskResult.list(detectTaskId: entity.id),
                        doneBuilder: (context, snapshot) => _GeneralTaskResultTable(
                          data: snapshot.requireData,
                          onDel: (id) => ModalUtil.confirm(content: "删除数据后不可恢复，确定要删除吗？").then((value) {
                            if (value == true) {
                              DBApi.generalTaskResult.delete(id).then((value) {
                                stateSetter(() {});
                                ToastUtil.success("删除成功");
                              });
                            }
                          }),
                        ),
                      ),
                    ),
                  ),
                ),
                separatorBuilder: (BuildContext context, int index) => Divider(height: Global.ds(10), color: Colors.transparent),
              ),
            ),
          ),
          _DetectPanel2(
            onGenerate: (radioId, startTime, endTime) {
              if (radioId == null) {
                ToastUtil.error("请选择探测台站");
              } else {
                DBApi.detectTaskBest.insertGeneral(radioId, startTime, endTime).then((value) {
                  if (value != null && value.isNotEmpty) {
                    ToastUtil.success("生成优选结果\n编号：$value");
                  } else {
                    ToastUtil.failure("生成失败");
                  }
                });
              }
            },
            builder: (radioId, startTime, endTime) => StatefulBuilder(
              builder: (context, stateSetter) => FutureWidget<List<GeneralTaskResultEntity>>(
                future: DBApi.generalTaskResult.list(radioId: radioId, startTime: startTime, endTime: endTime),
                doneBuilder: (context, snapshot) => _GeneralTaskResultTable(
                  data: snapshot.requireData,
                  onDel: (id) => ModalUtil.confirm(content: "删除数据后不可恢复，确定要删除吗？").then((value) {
                    if (value == true) {
                      DBApi.generalTaskResult.delete(id).then((value) {
                        stateSetter(() {});
                        ToastUtil.success("删除成功");
                      });
                    }
                  }),
                ),
              ),
            ),
          ),
          _DetectPanel3(
            builder: (int? radioId, DateTime? startTime, DateTime? endTime, stateSetter) => FutureWidget<List<DetectTaskBestEntity>>(
              future: DBApi.detectTaskBest.list(TaskType.general, radioId, startTime, endTime),
              doneBuilder: (context, snapshot) => CustomTable<DetectTaskBestEntity>(
                data: snapshot.requireData,
                fields: const [Text("编号"), Text("探测台站"), Text("生成时段"), Text("生成时间"), Text("操作")],
                shrinkWrap: true,
                headSize: Global.ds(50),
                itemSize: Global.ds(60),
                hoverColor: ColorUtil.color("#46E5CB").withOpacity(.5),
                headColor: ColorUtil.color("#1A263D"),
                itemBorder: Border(bottom: BorderSide(color: ColorUtil.color("#1A263D"))),
                headTextStyle: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#036EB7"), overflow: TextOverflow.ellipsis),
                itemTextStyle: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#2CA6E0"), overflow: TextOverflow.ellipsis),
                itemBuilder: (context, index, data) => [
                  Text(data.name),
                  Text(data.radioName ?? '未知'),
                  Text(data.startTime == null || data.endTime == null ? "-" : "${data.startTime}-${data.endTime}"),
                  Text(data.time),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      IconButton(
                        onPressed: () => ModalUtil.drawer(
                            context: context,
                            title: "${data.radioName}-优选结果",
                            radius: Global.ds(16),
                            resizeToAvoidBottomInset: false,
                            child: StatefulBuilder(
                              builder: (context, stateSetter) => FutureWidget<List<GeneralTaskResultEntity>>(
                                future: DBApi.detectTaskBest.generalResult(data.id!),
                                doneBuilder: (context, snapshot) => _GeneralTaskResultTable(
                                  data: snapshot.requireData,
                                  onDel: (id) => ModalUtil.confirm(content: "删除数据后不可恢复，确定要删除吗？").then((value) {
                                    if (value == true) {
                                      DBApi.generalTaskResult.delete(id).then((value) {
                                        stateSetter(() {});
                                        ToastUtil.success("删除成功");
                                      });
                                    }
                                  }),
                                ),
                              ),
                            )),
                        icon: ImageUtil.icon("static/images/icon/8.png"),
                        style: IconButton.styleFrom(iconSize: Global.ds(24), foregroundColor: ColorUtil.color("#FBD580")),
                      ),
                      VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                      IconButton(
                        onPressed: () => ModalUtil.confirm(content: "删除记录后不可恢复，确定要删除吗？").then((value) {
                          if (value == true) {
                            DBApi.detectTaskBest.delete(data.id!).then((value) {
                              stateSetter(() {});
                              ToastUtil.success("删除成功");
                            });
                          }
                        }),
                        icon: ImageUtil.icon("static/images/icon/6.png"),
                        style: IconButton.styleFrom(iconSize: Global.ds(24), fixedSize: Size.fromHeight(Global.ds(60)), foregroundColor: ColorUtil.color("#D95348")),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ][index],
      );
}

class DetectPick extends StatefulWidget {
  const DetectPick({super.key});

  @override
  State<DetectPick> createState() => _DetectPickState();
}

class _DetectPickState extends State<DetectPick> {
  @override
  Widget build(BuildContext context) => _DetectWrap(
        navList: const ["选频探测任务\n方案设置", "选频探测结果\n查询生成", "选频时段最优\n列表查询"],
        builder: (context, showPopover, index) => [
          _DetectPanel1(
            onAppend: (stateSetter) {
              const GlobalObjectKey bestKey = GlobalObjectKey("best");
              String? name, role, modemType, strategy = PickStrategy.once.name;
              int? radioId, power, disabledMHzId;
              DetectTaskBestEntity? best;
              List<DetectTaskBestEntity> bestOptions = [];
              ModalUtil.drawer(
                context: context,
                title: "添加选频探测方案",
                width: Global.ds(620),
                radius: Global.ds(16),
                barrierDismissible: false,
                child: Padding(
                  padding: EdgeInsets.all(Global.ds(20)),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("方案角色：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: StatefulBuilder(
                              builder: (context, stateSetter) => Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: DetectRole.values
                                    .map<RadioTile<String>>((e) => RadioTile<String>(
                                          value: e.name,
                                          groupValue: role,
                                          label: Text(e.label, style: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#FAFAFA"))),
                                          size: Global.ds(20),
                                          onCheck: (value) {
                                            stateSetter(() {
                                              role = value;
                                            });
                                          },
                                        ))
                                    .toList(),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("方案名称：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: InputField(
                                key: UniqueKey(),
                                hintText: "请输入方案名称",
                                textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                                hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                                onChanged: (controller, focusNode, value) => name = value.trim(),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("探测台站：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: FutureWidget(
                                future: DBApi.radio.list("id != ?", [context.read<StateNotifier>().localId]),
                                doneBuilder: (context, snapshot) => Dropdown<int>(
                                  key: UniqueKey(),
                                  options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text(e.name))).toList(),
                                  hintText: "请选择站台",
                                  fontSize: Global.ds(18),
                                  fontColor: ColorUtil.color("#ABDBF3"),
                                  dropdownColor: ColorUtil.color("#1A263D"),
                                  onChanged: (value) {
                                    if (value != radioId) best = null;
                                    DBApi.detectTaskBest.list(TaskType.general, value).then((value) => bestKey.currentState?.setState(() => bestOptions = value));
                                    radioId = value;
                                  },
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("优选频率：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: StatefulBuilder(
                                key: bestKey,
                                builder: (context, stateSetter) => Dropdown<DetectTaskBestEntity>(
                                  key: UniqueKey(),
                                  options: bestOptions.map((e) => DropdownMenuItem<DetectTaskBestEntity>(value: e, child: Text(e.name))).toList(),
                                  hintText: "请选择优选频率表",
                                  fontSize: Global.ds(18),
                                  fontColor: ColorUtil.color("#ABDBF3"),
                                  dropdownColor: ColorUtil.color("#1A263D"),
                                  onChanged: (value) => best = value,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("禁用频率：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: FutureWidget(
                                future: DBApi.disabledMHz.list(),
                                doneBuilder: (context, snapshot) => Dropdown<int>(
                                  key: UniqueKey(),
                                  options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text("禁用频率表V${e.id}"))).toList(),
                                  hintText: "请选择版本",
                                  fontSize: Global.ds(18),
                                  fontColor: ColorUtil.color("#ABDBF3"),
                                  dropdownColor: ColorUtil.color("#1A263D"),
                                  onChanged: (value) => disabledMHzId = value,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("探测功率：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: Dropdown<int>(
                                key: UniqueKey(),
                                options: PowerType.values.map((e) => DropdownMenuItem<int>(value: e.index, child: Text(e.name))).toList(),
                                hintText: "请选择功率",
                                fontSize: Global.ds(18),
                                fontColor: ColorUtil.color("#ABDBF3"),
                                dropdownColor: ColorUtil.color("#1A263D"),
                                onChanged: (value) => power = value,
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(height: Global.ds(20), color: Colors.transparent),
                      Row(
                        children: [
                          Container(
                            width: Global.ds(86),
                            height: Global.ds(40),
                            alignment: Alignment.centerRight,
                            child: Text("探测类型：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                          ),
                          Expanded(
                            child: Container(
                              height: Global.ds(40),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                              decoration: BoxDecoration(
                                color: ColorUtil.color("#366485"),
                                border: Border.all(color: ColorUtil.color("#3B99CC")),
                                borderRadius: BorderRadius.circular(Global.ds(20)),
                              ),
                              child: Dropdown<String>(
                                key: UniqueKey(),
                                options: ModemType.values.map((e) => DropdownMenuItem<String>(value: e.name, child: Text(e.name))).toList(),
                                hintText: "请选择类型",
                                fontSize: Global.ds(18),
                                fontColor: ColorUtil.color("#ABDBF3"),
                                dropdownColor: ColorUtil.color("#1A263D"),
                                onChanged: (value) => modemType = value,
                              ),
                            ),
                          ),
                        ],
                      ),
                      // Divider(height: Global.ds(20), color: Colors.transparent),
                      // Row(
                      //   children: [
                      //     Container(
                      //       width: Global.ds(86),
                      //       height: Global.ds(40),
                      //       alignment: Alignment.centerRight,
                      //       child: Text("发射策略：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                      //     ),
                      //     Expanded(
                      //       child: Container(
                      //         height: Global.ds(40),
                      //         alignment: Alignment.centerLeft,
                      //         padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                      //         decoration: BoxDecoration(
                      //           color: ColorUtil.color("#366485"),
                      //           border: Border.all(color: ColorUtil.color("#3B99CC")),
                      //           borderRadius: BorderRadius.circular(Global.ds(20)),
                      //         ),
                      //         child: Dropdown<String>(
                      //           key: UniqueKey(),
                      //           options: PickStrategy.values.map((e) => DropdownMenuItem<String>(value: e.name, child: Text(e.name))).toList(),
                      //           hintText: "请选择策略",
                      //           fontSize: Global.ds(18),
                      //           fontColor: ColorUtil.color("#ABDBF3"),
                      //           dropdownColor: ColorUtil.color("#1A263D"),
                      //           onChanged: (value) => strategy = value,
                      //         ),
                      //       ),
                      //     ),
                      //   ],
                      // ),
                      const Spacer(),
                      Align(
                        alignment: Alignment.centerRight,
                        child: TextButton(
                          onPressed: () {
                            if (name?.isEmpty ?? true) {
                              ToastUtil.error("请输入方案名称");
                            } else if (role == null) {
                              ToastUtil.error("请选择方案角色");
                            } else if (radioId == null) {
                              ToastUtil.error("请选择探测站台");
                            } else if (disabledMHzId == null) {
                              ToastUtil.error("请选择禁用频率");
                            } else if (power == null) {
                              ToastUtil.error("请选择探测功率");
                            } else if (modemType == null) {
                              ToastUtil.error("请选择探测类型");
                              // } else if (strategy == null) {
                              //   strategy = PickStrategy.once.name;
                              // ToastUtil.error("请选择发射策略");
                            } else {
                              String time = context.read<FpgaTimeNotifier>().format("yyyy-MM-dd HH:mm:ss");
                              LoadingUtil.show(
                                desc: "正在创建选频探测任务",
                                onLoading: (loading) {
                                  DBApi.detectTask.insert(DetectTaskEntity(radioId!, TaskType.pick.name, role!, name!, time, disabledMHzId!, 0)).then((value) {
                                    if (value != null) {
                                      DBApi.pickTaskHours.insert(value.id!, modemType!, power!, strategy, best?.resultIds ?? "").then((value) {
                                        stateSetter(() {});
                                        Navigator.pop(context);
                                        ToastUtil.success("添加成功");
                                        loading.close();
                                      });
                                    }
                                  });
                                },
                              );
                            }
                          },
                          style: TextButton.styleFrom(
                            fixedSize: Size(Global.ds(100), Global.ds(40)),
                            textStyle: TextStyle(fontSize: Global.ds(18)),
                            foregroundColor: Colors.white,
                            backgroundColor: ColorUtil.color("#2CA6E0"),
                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                          ),
                          child: const Text("确定"),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
            builder: (name, startTime, endTime, stateSetter) => FutureWidget<List<DetectTaskEntity>>(
              future: DBApi.detectTask.list(TaskType.pick, name, startTime, endTime),
              doneBuilder: (context, snapshot) => ListView.separated(
                itemCount: snapshot.requireData.length,
                itemBuilder: (BuildContext context, int index) => _DetectTaskItem(
                  entity: snapshot.requireData[index],
                  onTapDisabledMHz: (offset, taskId, values) {
                    showPopover(taskId, offset, Text(values.replaceAll(",", "、"), style: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#0F3A4E"))));
                  },
                  onEdit: (entity) => ModalUtil.drawer(
                    context: context,
                    title: "编辑选频探测方案",
                    width: Global.ds(620),
                    radius: Global.ds(16),
                    barrierDismissible: false,
                    child: Padding(
                      padding: EdgeInsets.all(Global.ds(20)),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          Row(
                            children: [
                              Container(
                                width: Global.ds(86),
                                height: Global.ds(40),
                                alignment: Alignment.centerRight,
                                child: Text("方案角色：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                              ),
                              Expanded(
                                child: StatefulBuilder(
                                  builder: (context, stateSetter) => Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: DetectRole.values
                                        .map<RadioTile<String>>((e) => RadioTile<String>(
                                              value: e.name,
                                              groupValue: entity.role,
                                              label: Text(e.label, style: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#FAFAFA"))),
                                              size: Global.ds(20),
                                              onCheck: (value) {
                                                stateSetter(() {
                                                  entity.role = value;
                                                });
                                              },
                                            ))
                                        .toList(),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          Divider(height: Global.ds(20), color: Colors.transparent),
                          Row(
                            children: [
                              Container(
                                width: Global.ds(86),
                                height: Global.ds(40),
                                alignment: Alignment.centerRight,
                                child: Text("方案名称：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                              ),
                              Expanded(
                                child: Container(
                                  height: Global.ds(40),
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                  decoration: BoxDecoration(
                                    color: ColorUtil.color("#366485"),
                                    border: Border.all(color: ColorUtil.color("#3B99CC")),
                                    borderRadius: BorderRadius.circular(Global.ds(20)),
                                  ),
                                  child: InputField(
                                    key: UniqueKey(),
                                    hintText: "请输入方案名称",
                                    initialValue: entity.name,
                                    textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                                    hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                                    onChanged: (controller, focusNode, value) => entity.name = value.trim(),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          Divider(height: Global.ds(20), color: Colors.transparent),
                          Row(
                            children: [
                              Container(
                                width: Global.ds(86),
                                height: Global.ds(40),
                                alignment: Alignment.centerRight,
                                child: Text("禁用频率：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                              ),
                              Expanded(
                                child: Container(
                                  height: Global.ds(40),
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                  decoration: BoxDecoration(
                                    color: ColorUtil.color("#366485"),
                                    border: Border.all(color: ColorUtil.color("#3B99CC")),
                                    borderRadius: BorderRadius.circular(Global.ds(20)),
                                  ),
                                  child: FutureWidget(
                                    future: DBApi.disabledMHz.list(),
                                    doneBuilder: (context, snapshot) => Dropdown<int>(
                                      key: UniqueKey(),
                                      options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text("禁用频率表V${e.id}"))).toList(),
                                      hintText: "请选择版本",
                                      initialValue: entity.disabledMHzId,
                                      fontSize: Global.ds(18),
                                      fontColor: ColorUtil.color("#ABDBF3"),
                                      dropdownColor: ColorUtil.color("#1A263D"),
                                      onChanged: (value) => entity.disabledMHzId = value!,
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          const Spacer(),
                          Align(
                            alignment: Alignment.centerRight,
                            child: TextButton(
                              onPressed: () {
                                if (entity.name.isEmpty) {
                                  ToastUtil.error("请输入方案名称");
                                } else {
                                  DBApi.detectTask.update(entity).then((value) {
                                    setState(() {});
                                    Navigator.pop(context);
                                    ToastUtil.success("修改成功");
                                  });
                                }
                              },
                              style: TextButton.styleFrom(
                                fixedSize: Size(Global.ds(100), Global.ds(40)),
                                textStyle: TextStyle(fontSize: Global.ds(18)),
                                foregroundColor: Colors.white,
                                backgroundColor: ColorUtil.color("#2CA6E0"),
                                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                              ),
                              child: const Text("确定"),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  onDel: (detectTaskId) => ModalUtil.confirm(content: "方案删除后不可恢复，确定要删除吗？").then((value) {
                    if (value == true) {
                      DBApi.detectTask.delete(detectTaskId).then((value) {
                        stateSetter(() {});
                        ToastUtil.success("删除成功");
                      });
                    }
                  }),
                  onHoursDetail: (entity) => ModalUtil.drawer(
                    context: context,
                    title: "24小时详情表-${entity.name}",
                    alignment: DrawerAlignment.start,
                    radius: Global.ds(16),
                    resizeToAvoidBottomInset: false,
                    child: StatefulBuilder(
                      builder: (context, stateSetter) => FutureWidget<List<PickTaskHoursEntity>>(
                        future: DBApi.pickTaskHours.list("detect_task_id = ?", [entity.id]),
                        doneBuilder: (context, snapshot) => CustomTable<PickTaskHoursEntity>(
                          data: snapshot.requireData,
                          fields: const [Text("时段"), Text("功率"), Text("探测类型"), Text("发射策略"), Text("探测频率"), Text("操作")],
                          headSize: Global.ds(40),
                          itemSize: Global.ds(80),
                          hoverColor: ColorUtil.color("#46E5CB").withOpacity(.5),
                          headColor: ColorUtil.color("#1A263D"),
                          itemBorder: Border(bottom: BorderSide(color: ColorUtil.color("#1A263D"))),
                          headTextStyle: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#036EB7"), overflow: TextOverflow.ellipsis),
                          itemTextStyle: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#2CA6E0"), overflow: TextOverflow.ellipsis),
                          itemBuilder: (context, index, data) => [
                            Text(data.hours),
                            Text(PowerType.values[data.power].name),
                            Text(data.modemType),
                            Text(data.strategy),
                            Text(data.hzs.isEmpty ? '-' : data.hzs.split(",").map((e) => Global.toFixed(double.parse(e) / 1000000, 3)).join(","), maxLines: 3),
                            IconButton(
                              onPressed: () {
                                String? modemType = data.modemType, strategy = data.strategy;
                                int? power = data.power;
                                List<double> hzs = data.hzs.isEmpty ? [] : data.hzs.split(",").map((e) => Global.toFixed(double.parse(e) / 1000000, 3)).toList();
                                bool added = false;
                                ModalUtil.drawer(
                                  context: context,
                                  title: "编辑时段(${data.hours})",
                                  width: Global.ds(620),
                                  radius: Global.ds(16),
                                  barrierDismissible: false,
                                  child: Padding(
                                    padding: EdgeInsets.all(Global.ds(20)),
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment: CrossAxisAlignment.stretch,
                                      children: [
                                        Row(
                                          children: [
                                            Container(
                                              width: Global.ds(86),
                                              height: Global.ds(40),
                                              alignment: Alignment.centerRight,
                                              child: Text(
                                                "探测功率：",
                                                style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")),
                                                textAlign: TextAlign.right,
                                              ),
                                            ),
                                            Expanded(
                                              child: Container(
                                                height: Global.ds(40),
                                                alignment: Alignment.centerLeft,
                                                padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                                decoration: BoxDecoration(
                                                  color: ColorUtil.color("#366485"),
                                                  border: Border.all(color: ColorUtil.color("#3B99CC")),
                                                  borderRadius: BorderRadius.circular(Global.ds(20)),
                                                ),
                                                child: Dropdown<int>(
                                                  key: UniqueKey(),
                                                  options: PowerType.values.map((e) => DropdownMenuItem<int>(value: e.index, child: Text(e.name))).toList(),
                                                  hintText: "请选择功率",
                                                  initialValue: power,
                                                  fontSize: Global.ds(18),
                                                  fontColor: ColorUtil.color("#ABDBF3"),
                                                  dropdownColor: ColorUtil.color("#1A263D"),
                                                  onChanged: (value) => power = value,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                        Divider(height: Global.ds(20), color: Colors.transparent),
                                        Row(
                                          children: [
                                            Container(
                                              width: Global.ds(86),
                                              height: Global.ds(40),
                                              alignment: Alignment.centerRight,
                                              child: Text("探测类型：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                                            ),
                                            Expanded(
                                              child: Container(
                                                height: Global.ds(40),
                                                alignment: Alignment.centerLeft,
                                                padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                                decoration: BoxDecoration(
                                                  color: ColorUtil.color("#366485"),
                                                  border: Border.all(color: ColorUtil.color("#3B99CC")),
                                                  borderRadius: BorderRadius.circular(Global.ds(20)),
                                                ),
                                                child: Dropdown<String>(
                                                  key: UniqueKey(),
                                                  options: ModemType.values.map((e) => DropdownMenuItem<String>(value: e.name, child: Text(e.name))).toList(),
                                                  hintText: "请选择类型",
                                                  initialValue: modemType,
                                                  fontSize: Global.ds(18),
                                                  fontColor: ColorUtil.color("#ABDBF3"),
                                                  dropdownColor: ColorUtil.color("#1A263D"),
                                                  onChanged: (value) => modemType = value,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                        Divider(height: Global.ds(20), color: Colors.transparent),
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              width: Global.ds(86),
                                              height: Global.ds(40),
                                              alignment: Alignment.centerRight,
                                              child: Text("探测频率：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                                            ),
                                            Expanded(
                                              child: StatefulBuilder(
                                                builder: (context, stateSetter) {
                                                  List<Widget> widgets = hzs
                                                      .asMap()
                                                      .keys
                                                      .map<Widget>((i) => Container(
                                                            height: Global.ds(40),
                                                            decoration: BoxDecoration(
                                                              color: ColorUtil.color("#2B526F"),
                                                              borderRadius: BorderRadius.circular(Global.ds(20)),
                                                            ),
                                                            child: Row(
                                                              mainAxisSize: MainAxisSize.min,
                                                              mainAxisAlignment: MainAxisAlignment.center,
                                                              children: [
                                                                VerticalDivider(width: Global.ds(15), color: Colors.transparent),
                                                                Text("${hzs[i]}", style: TextStyle(fontSize: Global.ds(18), color: Colors.white)),
                                                                IconButton(
                                                                  onPressed: () => stateSetter(() => hzs.removeAt(i)),
                                                                  icon: ImageUtil.icon("static/images/icon/close.png"),
                                                                  style: IconButton.styleFrom(iconSize: Global.ds(20), foregroundColor: ColorUtil.color("#FBD580")),
                                                                ),
                                                              ],
                                                            ),
                                                          ))
                                                      .toList();
                                                  if (widgets.length < 10) {
                                                    widgets.add(StatefulBuilder(builder: (context, addStateSetter) {
                                                      if (added) {
                                                        return Container(
                                                          width: Global.ds(160),
                                                          height: Global.ds(40),
                                                          alignment: Alignment.centerLeft,
                                                          padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                                          decoration: BoxDecoration(
                                                            color: ColorUtil.color("#366485"),
                                                            borderRadius: BorderRadius.circular(Global.ds(20)),
                                                          ),
                                                          child: InputField(
                                                            key: UniqueKey(),
                                                            hintText: "输入频率",
                                                            autofocus: true,
                                                            keyboardType: const TextInputType.numberWithOptions(decimal: true),
                                                            textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: Colors.white),
                                                            hintStyle: TextStyle(color: Colors.white.withOpacity(.5)),
                                                            suffix: const Text("MHz"),
                                                            onBlur: (controller, focusNode, value) => addStateSetter(() => added = false),
                                                            onSubmitted: (controller, focusNode, value) {
                                                              double? val = double.tryParse(value);
                                                              if (val != null) {
                                                                val = Global.toFixed(val, 3);
                                                                if (hzs.contains(val)) {
                                                                  ToastUtil.error("输入的值已存在");
                                                                } else {
                                                                  hzs.add(val);
                                                                  stateSetter(() => added = false);
                                                                }
                                                              } else {
                                                                ToastUtil.error("输入的值有误");
                                                              }
                                                            },
                                                          ),
                                                        );
                                                      } else {
                                                        return IconButton(
                                                          onPressed: () => addStateSetter(() => added = true),
                                                          icon: ImageUtil.icon("static/images/icon/plus.png"),
                                                          style: IconButton.styleFrom(
                                                            iconSize: Global.ds(24),
                                                            fixedSize: Size(Global.ds(120), Global.ds(40)),
                                                            foregroundColor: ColorUtil.color("#036EB7"),
                                                            backgroundColor: ColorUtil.color("#46E5CB"),
                                                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                                                          ),
                                                        );
                                                      }
                                                    }));
                                                  }
                                                  return Wrap(spacing: Global.ds(20), runSpacing: Global.ds(20), children: widgets);
                                                },
                                              ),
                                            ),
                                          ],
                                        ),
                                        // Divider(height: Global.ds(20), color: Colors.transparent),
                                        // Row(
                                        //   children: [
                                        //     Container(
                                        //       width: Global.ds(86),
                                        //       height: Global.ds(40),
                                        //       alignment: Alignment.centerRight,
                                        //       child: Text("发射策略：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#FAFAFA")), textAlign: TextAlign.right),
                                        //     ),
                                        //     Expanded(
                                        //       child: Container(
                                        //         height: Global.ds(40),
                                        //         alignment: Alignment.centerLeft,
                                        //         padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                                        //         decoration: BoxDecoration(
                                        //           color: ColorUtil.color("#366485"),
                                        //           border: Border.all(color: ColorUtil.color("#3B99CC")),
                                        //           borderRadius: BorderRadius.circular(Global.ds(20)),
                                        //         ),
                                        //         child: Dropdown<String>(
                                        //           key: UniqueKey(),
                                        //           options: PickStrategy.values.map((e) => DropdownMenuItem<String>(value: e.name, child: Text(e.name))).toList(),
                                        //           hintText: "请选择策略",
                                        //           fontSize: Global.ds(18),
                                        //           fontColor: ColorUtil.color("#ABDBF3"),
                                        //           dropdownColor: ColorUtil.color("#1A263D"),
                                        //           onChanged: (value) => strategy = value,
                                        //         ),
                                        //       ),
                                        //     ),
                                        //   ],
                                        // ),
                                        const Spacer(),
                                        Align(
                                          alignment: Alignment.centerRight,
                                          child: TextButton(
                                            onPressed: () {
                                              if (power == null) {
                                                ToastUtil.error("请选择探测功率");
                                              } else if (modemType == null) {
                                                ToastUtil.error("请选择探测类型");
                                              } else {
                                                data.power = power!;
                                                data.modemType = modemType!;
                                                data.strategy = strategy;
                                                data.hzs = (hzs.length > 10 ? hzs.sublist(0, 10) : hzs).map<int>((e) => (e * 1000000).toInt()).join(",");
                                                DBApi.pickTaskHours.update(data).then((value) {
                                                  stateSetter(() {});
                                                  Navigator.pop(context);
                                                  ToastUtil.success("修改成功");
                                                });
                                              }
                                            },
                                            style: TextButton.styleFrom(
                                              fixedSize: Size(Global.ds(100), Global.ds(40)),
                                              textStyle: TextStyle(fontSize: Global.ds(18)),
                                              foregroundColor: Colors.white,
                                              backgroundColor: ColorUtil.color("#2CA6E0"),
                                              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                                            ),
                                            child: const Text("确定"),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                              icon: ImageUtil.icon("static/images/icon/5.png"),
                              style: IconButton.styleFrom(iconSize: Global.ds(24), foregroundColor: ColorUtil.color("#66FFFB")),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  onResultDetail: (entity) => ModalUtil.drawer(
                    context: context,
                    title: "探测结果-${entity.name}",
                    alignment: DrawerAlignment.start,
                    radius: Global.ds(16),
                    resizeToAvoidBottomInset: false,
                    child: StatefulBuilder(
                      builder: (context, stateSetter) => FutureWidget<List<PickTaskResultEntity>>(
                        future: DBApi.pickTaskResult.list(detectTaskId: entity.id),
                        doneBuilder: (context, snapshot) => _PickTaskResultTable(
                          data: snapshot.requireData,
                          onDel: (id) => ModalUtil.confirm(content: "删除数据后不可恢复，确定要删除吗？").then((value) {
                            if (value == true) {
                              DBApi.pickTaskResult.delete(id).then((value) {
                                stateSetter(() {});
                                ToastUtil.success("删除成功");
                              });
                            }
                          }),
                        ),
                      ),
                    ),
                  ),
                ),
                separatorBuilder: (BuildContext context, int index) => Divider(height: Global.ds(10), color: Colors.transparent),
              ),
            ),
          ),
          _DetectPanel2(
            onGenerate: (radioId, startTime, endTime) {
              if (radioId == null) {
                ToastUtil.error("请选择探测台站");
              } else {
                DBApi.detectTaskBest.insertPick(radioId, startTime, endTime).then((value) {
                  if (value != null && value.isNotEmpty) {
                    ToastUtil.success("生成优选结果\n编号：$value");
                  } else {
                    ToastUtil.failure("生成失败");
                  }
                });
              }
            },
            builder: (radioId, startTime, endTime) => StatefulBuilder(
              builder: (context, stateSetter) => FutureWidget<List<PickTaskResultEntity>>(
                future: DBApi.pickTaskResult.list(radioId: radioId, startTime: startTime, endTime: endTime),
                doneBuilder: (context, snapshot) => _PickTaskResultTable(
                  data: snapshot.requireData,
                  onDel: (id) => ModalUtil.confirm(content: "删除数据后不可恢复，确定要删除吗？").then((value) {
                    if (value == true) {
                      DBApi.pickTaskResult.delete(id).then((value) {
                        stateSetter(() {});
                        ToastUtil.success("删除成功");
                      });
                    }
                  }),
                ),
              ),
            ),
          ),
          _DetectPanel3(
            builder: (int? radioId, DateTime? startTime, DateTime? endTime, stateSetter) => FutureWidget<List<DetectTaskBestEntity>>(
              future: DBApi.detectTaskBest.list(TaskType.pick, radioId, startTime, endTime),
              doneBuilder: (context, snapshot) => CustomTable<DetectTaskBestEntity>(
                data: snapshot.requireData,
                fields: const [Text("编号"), Text("探测台站"), Text("生成时段"), Text("生成时间"), Text("操作")],
                shrinkWrap: true,
                headSize: Global.ds(50),
                itemSize: Global.ds(60),
                hoverColor: ColorUtil.color("#46E5CB").withOpacity(.5),
                headColor: ColorUtil.color("#1A263D"),
                itemBorder: Border(bottom: BorderSide(color: ColorUtil.color("#1A263D"))),
                headTextStyle: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#036EB7"), overflow: TextOverflow.ellipsis),
                itemTextStyle: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#2CA6E0"), overflow: TextOverflow.ellipsis),
                itemBuilder: (context, index, data) => [
                  Text(data.name),
                  Text(data.radioName ?? '未知'),
                  Text(data.startTime == null || data.endTime == null ? "-" : "${data.startTime}-${data.endTime}"),
                  Text(data.time),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      IconButton(
                        onPressed: () => ModalUtil.drawer(
                            context: context,
                            title: "${data.radioName}-优选结果",
                            radius: Global.ds(16),
                            resizeToAvoidBottomInset: false,
                            child: StatefulBuilder(
                              builder: (context, stateSetter) => FutureWidget<List<PickTaskResultEntity>>(
                                future: DBApi.detectTaskBest.pickResult(data.id!),
                                doneBuilder: (context, snapshot) => _PickTaskResultTable(
                                  data: snapshot.requireData,
                                  onDel: (id) => ModalUtil.confirm(content: "删除数据后不可恢复，确定要删除吗？").then((value) {
                                    if (value == true) {
                                      DBApi.pickTaskResult.delete(id).then((value) {
                                        stateSetter(() {});
                                        ToastUtil.success("删除成功");
                                      });
                                    }
                                  }),
                                ),
                              ),
                            )),
                        icon: ImageUtil.icon("static/images/icon/8.png"),
                        style: IconButton.styleFrom(iconSize: Global.ds(24), foregroundColor: ColorUtil.color("#FBD580")),
                      ),
                      VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                      IconButton(
                        onPressed: () => ModalUtil.confirm(content: "删除记录后不可恢复，确定要删除吗？").then((value) {
                          if (value == true) {
                            DBApi.detectTaskBest.delete(data.id!).then((value) {
                              stateSetter(() {});
                              ToastUtil.success("删除成功");
                            });
                          }
                        }),
                        icon: ImageUtil.icon("static/images/icon/6.png"),
                        style: IconButton.styleFrom(iconSize: Global.ds(24), fixedSize: Size.fromHeight(Global.ds(60)), foregroundColor: ColorUtil.color("#D95348")),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ][index],
      );
}

class _DetectPanel1 extends StatefulWidget {
  final void Function(StateSetter stateSetter)? onAppend;
  final Widget Function(String? name, DateTime? startTime, DateTime? endTime, StateSetter stateSetter) builder;

  const _DetectPanel1({this.onAppend, required this.builder});

  @override
  State<_DetectPanel1> createState() => _DetectPanel1State();
}

class _DetectPanel1State extends State<_DetectPanel1> {
  String? name;
  DateTime? startTime, endTime;

  @override
  Widget build(BuildContext context) => Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          SizedBox(
            height: Global.ds(80),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("方案：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: Container(
                    height: Global.ds(40),
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                    decoration: BoxDecoration(
                      color: ColorUtil.color("#366485"),
                      border: Border.all(color: ColorUtil.color("#3B99CC")),
                      borderRadius: BorderRadius.circular(Global.ds(20)),
                    ),
                    child: InputField(
                      key: UniqueKey(),
                      initialValue: name ?? '',
                      hintText: "请输入方案名称",
                      textStyle: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                      hintStyle: TextStyle(color: ColorUtil.color("#ABDBF3").withOpacity(.5)),
                      onChanged: (controller, focusNode, value) => name = value.trim(),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                Text("始时：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: DatePicker(
                    value: startTime,
                    onChange: (result) => startTime = result,
                    builder: (context, result) => Container(
                      height: Global.ds(40),
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                      decoration: BoxDecoration(
                        color: ColorUtil.color("#366485"),
                        border: Border.all(color: ColorUtil.color("#3B99CC")),
                        borderRadius: BorderRadius.circular(Global.ds(20)),
                      ),
                      child: Text(result == null ? '请选择' : Global.parseTime(result) ?? '请选择', style: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3"))),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                ImageUtil.asset("static/images/icon/10.png", width: Global.ds(28), height: Global.ds(9)),
                VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                Text("终时：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: DatePicker(
                    value: endTime,
                    onChange: (result) => endTime = result,
                    builder: (context, result) => Container(
                      height: Global.ds(40),
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                      decoration: BoxDecoration(
                        color: ColorUtil.color("#366485"),
                        border: Border.all(color: ColorUtil.color("#3B99CC")),
                        borderRadius: BorderRadius.circular(Global.ds(20)),
                      ),
                      child: Text(result == null ? '请选择' : Global.parseTime(result) ?? '请选择', style: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3"))),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                TextButton(
                  onPressed: () => setState(() {}),
                  style: TextButton.styleFrom(
                    fixedSize: Size(Global.ds(80), Global.ds(40)),
                    foregroundColor: Colors.white,
                    backgroundColor: Colors.transparent,
                    textStyle: TextStyle(fontSize: Global.ds(18)),
                    shape: RoundedRectangleBorder(side: BorderSide(color: ColorUtil.color("#2CA6E0")), borderRadius: BorderRadius.circular(Global.ds(20))),
                  ),
                  child: const Text("查询"),
                ),
                VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                TextButton(
                  onPressed: () => widget.onAppend?.call(setState),
                  style: TextButton.styleFrom(
                    fixedSize: Size(Global.ds(80), Global.ds(40)),
                    foregroundColor: Colors.white,
                    disabledForegroundColor: Colors.white,
                    backgroundColor: ColorUtil.color("#2CA6E0"),
                    textStyle: TextStyle(fontSize: Global.ds(18)),
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                  ),
                  child: const Text("添加"),
                ),
              ],
            ),
          ),
          Expanded(child: widget.builder(name, startTime, endTime, setState)),
        ],
      );
}

class _DetectPanel2 extends StatefulWidget {
  final void Function(int? radioId, DateTime? startTime, DateTime? endTime)? onGenerate;
  final Widget Function(int? radioId, DateTime? startTime, DateTime? endTime) builder;

  const _DetectPanel2({this.onGenerate, required this.builder});

  @override
  State<_DetectPanel2> createState() => _DetectPanel2State();
}

class _DetectPanel2State extends State<_DetectPanel2> {
  int? radioId;
  DateTime? startTime, endTime;

  @override
  Widget build(BuildContext context) => Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          SizedBox(
            height: Global.ds(80),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("探测台站：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: Container(
                    height: Global.ds(40),
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                    decoration: BoxDecoration(
                      color: ColorUtil.color("#366485"),
                      border: Border.all(color: ColorUtil.color("#3B99CC")),
                      borderRadius: BorderRadius.circular(Global.ds(20)),
                    ),
                    child: FutureWidget<List<RadioEntity>>(
                      future: DBApi.radio.list("`id` != ?", [context.read<StateNotifier>().localId]),
                      doneBuilder: (context, snapshot) => Dropdown<int>(
                        key: UniqueKey(),
                        options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text(e.name))).toList(),
                        initialValue: radioId,
                        hintText: "请选择台站",
                        dropdownColor: ColorUtil.color("#1A263D"),
                        fontSize: Global.ds(18),
                        fontColor: ColorUtil.color("#ABDBF3"),
                        onChanged: (value) => radioId = value,
                      ),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                Text("始时：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: DatePicker(
                    value: startTime,
                    mode: CupertinoDatePickerMode.date,
                    onChange: (result) => startTime = result,
                    builder: (context, result) => Container(
                      height: Global.ds(40),
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                      decoration: BoxDecoration(
                        color: ColorUtil.color("#366485"),
                        border: Border.all(color: ColorUtil.color("#3B99CC")),
                        borderRadius: BorderRadius.circular(Global.ds(20)),
                      ),
                      child: Text(
                        result == null ? '请选择' : Global.parseTime(result, resolve: 'yyyy-MM-dd') ?? '请选择',
                        style: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                      ),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                ImageUtil.asset("static/images/icon/10.png", width: Global.ds(28), height: Global.ds(9)),
                VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                Text("终时：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: DatePicker(
                    value: endTime,
                    mode: CupertinoDatePickerMode.date,
                    onChange: (result) => endTime = result,
                    builder: (context, result) => Container(
                      height: Global.ds(40),
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                      decoration: BoxDecoration(
                        color: ColorUtil.color("#366485"),
                        border: Border.all(color: ColorUtil.color("#3B99CC")),
                        borderRadius: BorderRadius.circular(Global.ds(20)),
                      ),
                      child: Text(
                        result == null ? '请选择' : Global.parseTime(result, resolve: 'yyyy-MM-dd') ?? '请选择',
                        style: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3")),
                      ),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                TextButton(
                  onPressed: () => setState(() {}),
                  style: TextButton.styleFrom(
                    fixedSize: Size(Global.ds(80), Global.ds(40)),
                    foregroundColor: Colors.white,
                    backgroundColor: Colors.transparent,
                    textStyle: TextStyle(fontSize: Global.ds(18)),
                    shape: RoundedRectangleBorder(side: BorderSide(color: ColorUtil.color("#2CA6E0")), borderRadius: BorderRadius.circular(Global.ds(20))),
                  ),
                  child: const Text("查询"),
                ),
                VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                TextButton(
                  onPressed: () => widget.onGenerate?.call(radioId, startTime, endTime),
                  style: TextButton.styleFrom(
                    fixedSize: Size(Global.ds(80), Global.ds(40)),
                    foregroundColor: Colors.white,
                    disabledForegroundColor: Colors.white,
                    backgroundColor: ColorUtil.color("#2CA6E0"),
                    textStyle: TextStyle(fontSize: Global.ds(18)),
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                  ),
                  child: const Text("生成"),
                ),
              ],
            ),
          ),
          Expanded(child: widget.builder(radioId, startTime, endTime)),
        ],
      );
}

class _DetectPanel3 extends StatefulWidget {
  final Widget Function(int? radioId, DateTime? startTime, DateTime? endTime, StateSetter stateSetter) builder;

  const _DetectPanel3({required this.builder});

  @override
  State<_DetectPanel3> createState() => _DetectPanel3State();
}

class _DetectPanel3State extends State<_DetectPanel3> {
  int? radioId;
  DateTime? startTime, endTime;

  @override
  Widget build(BuildContext context) => Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          SizedBox(
            height: Global.ds(80),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("探测台站：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: Container(
                    height: Global.ds(40),
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                    decoration: BoxDecoration(
                      color: ColorUtil.color("#366485"),
                      border: Border.all(color: ColorUtil.color("#3B99CC")),
                      borderRadius: BorderRadius.circular(Global.ds(20)),
                    ),
                    child: FutureWidget<List<RadioEntity>>(
                      future: DBApi.radio.list("`id` != ?", [context.read<StateNotifier>().localId]),
                      doneBuilder: (context, snapshot) => Dropdown<int>(
                        key: UniqueKey(),
                        options: snapshot.requireData.map((e) => DropdownMenuItem<int>(value: e.id, child: Text(e.name))).toList(),
                        initialValue: radioId,
                        hintText: "请选择台站",
                        fontSize: Global.ds(18),
                        fontColor: ColorUtil.color("#ABDBF3"),
                        dropdownColor: ColorUtil.color("#1A263D"),
                        onChanged: (value) => radioId = value,
                      ),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                Text("始时：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: DatePicker(
                    value: startTime,
                    mode: CupertinoDatePickerMode.date,
                    onChange: (result) => startTime = result,
                    builder: (context, result) => Container(
                      height: Global.ds(40),
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                      decoration: BoxDecoration(
                        color: ColorUtil.color("#366485"),
                        border: Border.all(color: ColorUtil.color("#3B99CC")),
                        borderRadius: BorderRadius.circular(Global.ds(20)),
                      ),
                      child: Text(result == null ? '请选择' : Global.parseTime(result) ?? '请选择', style: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3"))),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                ImageUtil.asset("static/images/icon/10.png", width: Global.ds(28), height: Global.ds(9)),
                VerticalDivider(width: Global.ds(5), color: Colors.transparent),
                Text("终时：", style: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#F4F4F4"))),
                Expanded(
                  child: DatePicker(
                    value: endTime,
                    mode: CupertinoDatePickerMode.date,
                    onChange: (result) => endTime = result,
                    builder: (context, result) => Container(
                      height: Global.ds(40),
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.symmetric(horizontal: Global.ds(15)),
                      decoration: BoxDecoration(
                        color: ColorUtil.color("#366485"),
                        border: Border.all(color: ColorUtil.color("#3B99CC")),
                        borderRadius: BorderRadius.circular(Global.ds(20)),
                      ),
                      child: Text(result == null ? '请选择' : Global.parseTime(result) ?? '请选择', style: TextStyle(fontSize: Global.ds(18), height: 1, color: ColorUtil.color("#ABDBF3"))),
                    ),
                  ),
                ),
                VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                TextButton(
                  onPressed: () => setState(() {}),
                  style: TextButton.styleFrom(
                    fixedSize: Size(Global.ds(80), Global.ds(40)),
                    foregroundColor: Colors.white,
                    disabledForegroundColor: Colors.white,
                    backgroundColor: ColorUtil.color("#2CA6E0"),
                    textStyle: TextStyle(fontSize: Global.ds(18)),
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Global.ds(20))),
                  ),
                  child: const Text("查询"),
                ),
              ],
            ),
          ),
          Expanded(child: widget.builder(radioId, startTime, endTime, setState)),
        ],
      );
}

class _DetectTaskItem extends StatefulWidget {
  final DetectTaskEntity entity;
  final void Function(Offset offset, int taskId, String values) onTapDisabledMHz;
  final void Function(DetectTaskEntity entity) onEdit;
  final void Function(int detectTaskId) onDel;
  final void Function(DetectTaskEntity entity) onHoursDetail;
  final void Function(DetectTaskEntity entity) onResultDetail;

  const _DetectTaskItem({
    required this.entity,
    required this.onTapDisabledMHz,
    required this.onDel,
    required this.onEdit,
    required this.onHoursDetail,
    required this.onResultDetail,
  });

  @override
  State<_DetectTaskItem> createState() => _DetectTaskItemState();
}

class _DetectTaskItemState extends State<_DetectTaskItem> {
  @override
  Widget build(BuildContext context) => SizedBox(
        height: Global.ds(96),
        child: Stack(
          alignment: Alignment.centerLeft,
          children: [
            Positioned.fill(
              child: Container(
                padding: EdgeInsets.only(left: Global.ds(20), top: Global.ds(10), right: Global.ds(85), bottom: Global.ds(10)),
                decoration: BoxDecoration(color: ColorUtil.color("#2F557C"), borderRadius: BorderRadius.circular(Global.ds(16))),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      widget.entity.name,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(fontSize: Global.ds(24), fontWeight: FontWeight.w700, color: ColorUtil.color("#74C8CE")),
                    ),
                    Row(mainAxisSize: MainAxisSize.min, children: [
                      Text.rich(
                        TextSpan(children: [
                          TextSpan(text: "探测站点：", style: TextStyle(color: ColorUtil.color("#6D88A3"))),
                          TextSpan(text: widget.entity.radioName ?? '无', style: TextStyle(color: ColorUtil.color("#ABDBF3"))),
                        ]),
                        style: TextStyle(fontSize: Global.ds(18)),
                      ),
                      VerticalDivider(width: Global.ds(40), color: Colors.transparent),
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTapUp: (TapUpDetails details) => widget.onTapDisabledMHz.call(details.globalPosition, widget.entity.id!, widget.entity.disabledMHzValues!),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text.rich(
                              TextSpan(children: [
                                TextSpan(text: "禁用频率：", style: TextStyle(color: ColorUtil.color("#6D88A3"))),
                                TextSpan(text: "V${widget.entity.disabledMHzId}", style: TextStyle(color: ColorUtil.color("#ABDBF3"))),
                              ]),
                              style: TextStyle(fontSize: Global.ds(18)),
                            ),
                            VerticalDivider(width: Global.ds(10), color: Colors.transparent),
                            Transform.rotate(
                              angle: pi * .5,
                              filterQuality: FilterQuality.high,
                              child: Icon(Icons.arrow_forward_ios, size: Global.ds(18), color: ColorUtil.color("#ABDBF3")),
                            ),
                          ],
                        ),
                      ),
                      const Spacer(),
                      Text(widget.entity.time, style: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#74C8CE"))),
                    ]),
                  ],
                ),
              ),
            ),
            Positioned(
              top: 0,
              bottom: 0,
              right: 0,
              width: Global.ds(500),
              child: ScrollListener(
                refreshOnScroll: true,
                onNotification: (controller, prev, current) {
                  double max = controller.position.maxScrollExtent, offset = controller.offset;
                  bool isNotification = (prev is ScrollEndNotification && current is UserScrollNotification) || current is ScrollEndNotification;
                  if (isNotification && offset > 0 && offset < max) {
                    Future<void>.delayed(Duration.zero, () async {
                      controller.animateTo(offset >= max / 2 ? max : 0, duration: const Duration(milliseconds: 300), curve: Curves.easeInOut);
                    });
                  }
                  return false;
                },
                builder: (context, controller) {
                  double dx = controller.hasClients ? controller.offset / controller.position.maxScrollExtent : 0;
                  return SingleChildScrollView(
                    padding: EdgeInsets.zero,
                    scrollDirection: Axis.horizontal,
                    controller: controller,
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        SizedBox(width: Global.ds(425)),
                        Container(
                          clipBehavior: Clip.antiAlias,
                          decoration: BoxDecoration(color: ColorUtil.color("#35689C"), borderRadius: BorderRadius.circular(Global.ds(16))),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            children: [
                              GestureDetector(
                                onTap: () => controller.animateTo(
                                  dx >= .5 ? 0 : controller.position.maxScrollExtent,
                                  duration: const Duration(milliseconds: 300),
                                  curve: Curves.easeInOut,
                                ),
                                child: Container(
                                  clipBehavior: Clip.hardEdge,
                                  decoration: BoxDecoration(color: ColorUtil.color("#3B99CC")),
                                  child: SizedOverflowBox(
                                    size: Size.fromWidth(Global.ds(75)),
                                    alignment: Alignment.centerLeft,
                                    child: Transform.translate(
                                      filterQuality: FilterQuality.high,
                                      offset: Offset(-dx * Global.ds(75), 0),
                                      child: Row(
                                        mainAxisSize: MainAxisSize.min,
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        crossAxisAlignment: CrossAxisAlignment.stretch,
                                        children: [
                                          SizedBox(
                                            width: Global.ds(75),
                                            child: Column(
                                              mainAxisSize: MainAxisSize.min,
                                              mainAxisAlignment: MainAxisAlignment.center,
                                              children: [
                                                Icon(Icons.arrow_back_ios, size: Global.ds(18), color: Colors.white),
                                                Divider(height: Global.ds(5), color: Colors.transparent),
                                                Text("操作", style: TextStyle(fontSize: Global.ds(14), color: Colors.white)),
                                              ],
                                            ),
                                          ),
                                          SizedBox(
                                            width: Global.ds(75),
                                            child: Column(
                                              mainAxisSize: MainAxisSize.min,
                                              mainAxisAlignment: MainAxisAlignment.center,
                                              children: [
                                                Icon(Icons.arrow_forward_ios, size: Global.ds(18), color: Colors.white),
                                                Divider(height: Global.ds(5), color: Colors.transparent),
                                                Text("返回", style: TextStyle(fontSize: Global.ds(14), color: Colors.white)),
                                              ],
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              SizedBox(
                                width: Global.ds(425),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  children: [
                                    MaterialButton(
                                      onPressed: () => widget.onEdit(widget.entity),
                                      padding: EdgeInsets.symmetric(horizontal: Global.ds(10)),
                                      child: Column(
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          SizedBox(
                                            width: Global.ds(30),
                                            height: Global.ds(30),
                                            child: Align(child: ImageUtil.icon("static/images/icon/5.png", size: Global.ds(Global.ds(24)), color: ColorUtil.color("#66FFFB"))),
                                          ),
                                          Text("编辑", style: TextStyle(fontSize: Global.ds(12), color: Colors.white)),
                                        ],
                                      ),
                                    ),
                                    MaterialButton(
                                      onPressed: () => widget.onDel(widget.entity.id!),
                                      padding: EdgeInsets.symmetric(horizontal: Global.ds(10)),
                                      child: Column(
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          SizedBox(
                                            width: Global.ds(30),
                                            height: Global.ds(30),
                                            child: Align(child: ImageUtil.icon("static/images/icon/6.png", size: Global.ds(Global.ds(24)), color: ColorUtil.color("#D95348"))),
                                          ),
                                          Text("删除", style: TextStyle(fontSize: Global.ds(12), color: Colors.white))
                                        ],
                                      ),
                                    ),
                                    MaterialButton(
                                      onPressed: () => widget.onHoursDetail(widget.entity),
                                      padding: EdgeInsets.symmetric(horizontal: Global.ds(10)),
                                      child: Column(
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          SizedBox(
                                            width: Global.ds(30),
                                            height: Global.ds(30),
                                            child: Align(child: ImageUtil.icon("static/images/icon/7.png", size: Global.ds(Global.ds(24)), color: ColorUtil.color("#FBD580"))),
                                          ),
                                          Text("24小时详情", style: TextStyle(fontSize: Global.ds(12), color: Colors.white))
                                        ],
                                      ),
                                    ),
                                    StatefulBuilder(
                                      builder: (context, stateSetter) => MaterialButton(
                                        onPressed: () async {
                                          DetectTaskEntity? detectTaskEntity = await DBApi.detectTask.select(widget.entity.id!);
                                          if (detectTaskEntity == null) {
                                            ToastUtil.error("任务不存在");
                                          } else if (!DetectUtil.instance.isDetecting()) {
                                            LoadingUtil.show(onLoading: (loading) {
                                              DetectUtil.instance.start(detectTaskEntity).then((value) {
                                                stateSetter(() {});
                                                loading.close();
                                              });
                                            });
                                          } else if (DetectUtil.instance.isDetecting(widget.entity.id!)) {
                                            LoadingUtil.show(onLoading: (loading) {
                                              DetectUtil.instance.stop().then((value) {
                                                stateSetter(() {});
                                                loading.close();
                                              });
                                            });
                                          } else {
                                            ToastUtil.msg("请先停止其他探测任务再操作");
                                          }
                                        },
                                        padding: EdgeInsets.symmetric(horizontal: Global.ds(10)),
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: [
                                            Container(
                                              width: Global.ds(90),
                                              height: Global.ds(30),
                                              decoration: BoxDecoration(
                                                color: ColorUtil.color(DetectUtil.instance.isDetecting(widget.entity.id) ? "#F39800" : "#036EB7"),
                                                border: Border.all(color: ColorUtil.color(DetectUtil.instance.isDetecting(widget.entity.id) ? "#FBD580" : "#74C8CE")),
                                                borderRadius: BorderRadius.circular(Global.ds(15)),
                                              ),
                                              child: Row(
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                children: [
                                                  ImageUtil.icon(
                                                    "static/images/state/state.${DetectUtil.instance.isDetecting(widget.entity.id) ? "pause" : "play"}.png",
                                                    size: Global.ds(16),
                                                    color: Colors.white,
                                                  ),
                                                  Text(
                                                    DetectUtil.instance.isDetecting(widget.entity.id) ? "停止探测" : "启动探测",
                                                    style: TextStyle(fontSize: Global.ds(12), height: 1, color: Colors.white),
                                                  ),
                                                ],
                                              ),
                                            ),
                                            Text(widget.entity.isDetect ? "已探测" : "未探测", style: TextStyle(fontSize: Global.ds(12), color: Colors.white)),
                                          ],
                                        ),
                                      ),
                                    ),
                                    MaterialButton(
                                      onPressed: () => widget.onResultDetail(widget.entity),
                                      padding: EdgeInsets.symmetric(horizontal: Global.ds(10)),
                                      child: Column(
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          SizedBox(
                                            width: Global.ds(30),
                                            height: Global.ds(30),
                                            child: Align(child: ImageUtil.icon("static/images/icon/8.png", size: Global.ds(Global.ds(24)), color: ColorUtil.color("#FBD580"))),
                                          ),
                                          Text("探测结果", style: TextStyle(fontSize: Global.ds(12), color: Colors.white))
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      );
}

class _GeneralTaskResultTable extends StatelessWidget {
  final List<GeneralTaskResultEntity> data;
  final void Function(int id) onDel;

  const _GeneralTaskResultTable({required this.data, required this.onDel});

  @override
  Widget build(BuildContext context) => CustomTable<GeneralTaskResultEntity>(
        data: data,
        fields: const [
          Text("时段"),
          Text("探测时间"),
          Text("探测台站"),
          Text("探测角色"),
          Text("通道"),
          Text("频率"),
          Text("信号幅度"),
          Text("噪声幅度"),
          Text("多普勒"),
          Text("多径"),
          Text("操作"),
        ],
        direction: Axis.horizontal,
        headSize: Global.ds(100),
        itemSize: Global.ds(200),
        hoverColor: ColorUtil.color("#46E5CB").withOpacity(.5),
        headColor: ColorUtil.color("#1A263D"),
        itemCellBorder: Border(right: BorderSide(color: ColorUtil.color("#1A263D")), bottom: BorderSide(color: ColorUtil.color("#1A263D"))),
        headCellPadding: EdgeInsets.symmetric(horizontal: Global.ds(10)),
        itemCellPadding: EdgeInsets.all(Global.ds(10)),
        headTextStyle: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#036EB7")),
        itemTextStyle: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#2CA6E0")),
        itemBuilder: (context, index, data) => [
          Text(data.hours),
          Text(data.time),
          Text(data.detectTask!.radioName!),
          Text(DetectRole.values.byName(data.detectTask!.role).label),
          Text(data.channel.toString()),
          Text(Global.toFixed(data.hz / 1000000, 3).toString()),
          Text(Global.toFixed(data.signal, 3).toString()),
          Text(Global.toFixed(data.noise, 3).toString()),
          Text(Global.toFixed(data.doppler, 3).toString()),
          Text(Global.toFixed(data.multiPath, 3).toString()),
          IconButton(
            onPressed: () => onDel(data.id!),
            icon: ImageUtil.icon("static/images/icon/6.png"),
            style: IconButton.styleFrom(iconSize: Global.ds(24), fixedSize: Size(Global.ds(120), Global.ds(60)), foregroundColor: ColorUtil.color("#D95348")),
          ),
        ],
      );
}

class _PickTaskResultTable extends StatelessWidget {
  final List<PickTaskResultEntity> data;
  final void Function(int id) onDel;

  const _PickTaskResultTable({required this.data, required this.onDel});

  @override
  Widget build(BuildContext context) => CustomTable<PickTaskResultEntity>(
        data: data,
        fields: const [Text("时段"), Text("探测时间"), Text("探测台站"), Text("频率"), Text("信噪比"), Text("同步率"), Text("丢包率"), Text("操作")],
        direction: Axis.horizontal,
        headSize: Global.ds(100),
        itemSize: Global.ds(200),
        hoverColor: ColorUtil.color("#46E5CB").withOpacity(.5),
        headColor: ColorUtil.color("#1A263D"),
        itemCellBorder: Border(right: BorderSide(color: ColorUtil.color("#1A263D")), bottom: BorderSide(color: ColorUtil.color("#1A263D"))),
        headCellPadding: EdgeInsets.symmetric(horizontal: Global.ds(10)),
        itemCellPadding: EdgeInsets.all(Global.ds(10)),
        headTextStyle: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#036EB7")),
        itemTextStyle: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#2CA6E0")),
        itemBuilder: (context, index, data) => [
          Text(data.hours),
          Text(data.time),
          Text(data.detectTask!.radioName!),
          Text(Global.toFixed(data.hz / 1000000, 3).toString()),
          Text(Global.toFixed(data.snrC0, 3).toString()),
          Text(Global.toFixed(data.syncC0, 3).toString()),
          Text(Global.toFixed(data.lostC0, 3).toString()),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              IconButton(
                onPressed: () => ModalUtil.drawer(
                  context: context,
                  title: "数据包详情",
                  alignment: DrawerAlignment.start,
                  radius: Global.ds(16),
                  resizeToAvoidBottomInset: false,
                  child: StatefulBuilder(
                    builder: (context, stateSetter) => FutureWidget<List<PickTaskResultPackageEntity>>(
                      future: DBApi.pickTaskResultPackage.list(data.id!),
                      doneBuilder: (context, snapshot) => CustomTable<PickTaskResultPackageEntity>(
                        data: snapshot.requireData,
                        fields: const [
                          Text("时段"),
                          Text("探测时间"),
                          Text("探测台站"),
                          Text("探测类型"),
                          Text("通道"),
                          Text("数据"),
                          Text("频率"),
                          Text("操作"),
                        ],
                        direction: Axis.horizontal,
                        headSize: Global.ds(100),
                        itemSize: Global.ds(200),
                        hoverColor: ColorUtil.color("#46E5CB").withOpacity(.5),
                        headColor: ColorUtil.color("#1A263D"),
                        itemCellBorder: Border(right: BorderSide(color: ColorUtil.color("#1A263D")), bottom: BorderSide(color: ColorUtil.color("#1A263D"))),
                        headCellPadding: EdgeInsets.symmetric(horizontal: Global.ds(10)),
                        itemCellPadding: EdgeInsets.all(Global.ds(10)),
                        headTextStyle: TextStyle(fontSize: Global.ds(16), color: ColorUtil.color("#036EB7")),
                        itemTextStyle: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#2CA6E0")),
                        itemBuilder: (context, index, data) => [
                          Text(data.hours),
                          Text(data.time),
                          Text(data.detectTask!.radioName!),
                          Text(data.modemType),
                          Text(data.channel.toString()),
                          Text(data.data, maxLines: 1),
                          Text(Global.toFixed(data.hz / 1000000, 3).toString()),
                          IconButton(
                            onPressed: () => onDel(data.id!),
                            icon: ImageUtil.icon("static/images/icon/6.png"),
                            style: IconButton.styleFrom(iconSize: Global.ds(24), fixedSize: Size.fromHeight(Global.ds(60)), foregroundColor: ColorUtil.color("#D95348")),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                icon: ImageUtil.icon("static/images/icon/8.png"),
                style: IconButton.styleFrom(iconSize: Global.ds(24), fixedSize: Size.fromHeight(Global.ds(60)), foregroundColor: ColorUtil.color("#FBD580")),
              ),
              VerticalDivider(width: Global.ds(10), color: Colors.transparent),
              IconButton(
                onPressed: () => onDel(data.id!),
                icon: ImageUtil.icon("static/images/icon/6.png"),
                style: IconButton.styleFrom(iconSize: Global.ds(24), fixedSize: Size.fromHeight(Global.ds(60)), foregroundColor: ColorUtil.color("#D95348")),
              ),
            ],
          ),
        ],
      );
}

class _DetectWrap extends StatefulWidget {
  final List<String> navList;
  final Widget Function(BuildContext context, void Function(int id, Offset offset, Widget widget) showPopover, int index) builder;

  const _DetectWrap({required this.navList, required this.builder});

  @override
  State<_DetectWrap> createState() => _DetectWrapState();
}

class _DetectWrapState extends State<_DetectWrap> {
  final GlobalKey _popoverKey = GlobalKey();
  int? _popoverId;
  Offset? _popoverOffset;
  Widget? _popoverWidget;
  int index = 0;

  @override
  Widget build(BuildContext context) => Stack(
        alignment: Alignment.center,
        children: [
          GestureDetector(
            onTap: () => _popoverKey.currentState?.setState(() {
              _popoverId = null;
            }),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  decoration: BoxDecoration(color: ColorUtil.color("#153252"), borderRadius: BorderRadius.horizontal(left: Radius.circular(Global.ds(30)))),
                  padding: EdgeInsets.symmetric(vertical: Global.ds(60)),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: widget.navList
                        .asMap()
                        .keys
                        .map<Widget>((i) => GestureDetector(
                              onTap: () => setState(() => index = i),
                              child: Padding(
                                padding: EdgeInsets.all(Global.ds(20)),
                                child: Text(widget.navList[i], style: TextStyle(fontSize: Global.ds(18), color: index == i ? Colors.white : ColorUtil.color("#9E9E9E"))),
                              ),
                            ))
                        .toList(),
                  ),
                ),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: Global.ds(20)),
                    decoration: BoxDecoration(
                      color: ColorUtil.color("#153252"),
                      borderRadius: BorderRadius.only(topRight: Radius.circular(Global.ds(30)), bottomLeft: Radius.circular(Global.ds(30)), bottomRight: Radius.circular(Global.ds(30))),
                    ),
                    child: widget.builder(context, (int id, Offset offset, Widget widget) {
                      if (id != _popoverId) {
                        _popoverKey.currentState?.setState(() {
                          RenderBox renderBox = context.findRenderObject() as RenderBox;
                          _popoverOffset = offset - renderBox.localToGlobal(Offset.zero);
                          _popoverWidget = widget;
                          _popoverId = id;
                        });
                      }
                    }, index),
                  ),
                ),
              ],
            ),
          ),
          Positioned.fill(
            child: StatefulBuilder(
              key: _popoverKey,
              builder: (context, stateSetter) => Stack(
                children: [
                  Positioned(
                    left: (_popoverOffset?.dx ?? 0) - Global.ds(200 / 2),
                    top: _popoverOffset?.dy,
                    width: Global.ds(200),
                    child: AnimatedSwitcher(
                      switchInCurve: Curves.fastOutSlowIn,
                      switchOutCurve: Curves.fastOutSlowIn,
                      duration: const Duration(milliseconds: 300),
                      transitionBuilder: (child, animation) => SizeTransition(sizeFactor: animation, child: child),
                      child: _popoverId == null
                          ? null
                          : Center(
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  Triangle(size: Global.ds(10), color: ColorUtil.color("#2CA6E0"), direction: TriangleDirection.top),
                                  Container(
                                    clipBehavior: Clip.antiAlias,
                                    padding: EdgeInsets.all(Global.ds(10)),
                                    decoration: BoxDecoration(color: ColorUtil.color("#2CA6E0"), borderRadius: BorderRadius.circular(Global.ds(16))),
                                    child: _popoverWidget,
                                  ),
                                ],
                              ),
                            ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      );
}
